package com.algorithm;

/**
 * @author: sun.hongliang
 * @create: 2019/07/24 11:45
 */
public class StringIndex {
    public static void main(String[] args) {

    }

    public int strStr(String haystack, String needle) {
        if (needle == null || needle.equals("")) {
            return 0;
        }
        int result = -1;
        int length = haystack.length();
        int needLength = needle.length();
        for (int i = 0; i < length - needLength + 1; i++) {
            if (haystack.charAt(i) != needle.charAt(0)) {
                continue;
            }
            boolean flg = true;
            for (int j = 0; j < needLength; j++) {
                if (haystack.charAt(j + i) != needle.charAt(j)) {
                    flg = false;
                    break;
                }
            }
            if (flg) {
                result = i;
                break;
            }
        }
        return result;
    }
}
